import emojimap from './emojimap.js'
import md5Libs from "uview-ui/libs/function/md5"; //引入md5加密方式
// const baseUrl = uni.getStorageSync('baseUrl');
import {DEVELOPMENTAPI} from '../config/index'


const emoji = emojimap.emojiList.emoji

const md5 = md5Libs.md5;
const baseUrl = DEVELOPMENTAPI
const token = uni.getStorageSync("token");




/*  #ifdef  APP-PLUS  */
    // const dcRichAlert = uni.requireNativePlugin('DCloud-RichAlert')
    /*  #endif  */
const showToast = msg => {
    uni.showToast({
        title: msg,
        duration: 2000,
        position: 'center',
        icon: 'none'
    })
}

const showLoading = title => {
    uni.showLoading({
        title: title,
        mask: true,
    })
}

const hideLoading = () => {
    uni.hideLoading()
}

const share = (params) => {
    uni.showActionSheet({
        itemList: ['微信分享', '微信朋友圈分享', 'QQ分享'],
        success: (res) => {
            const index = res.tapIndex
            const provider = index === 2 ? 'qq' : 'weixin'
            let shareData = {}

            if (index === 2) {
                shareData = {
                    provider: provider,
                    // ...params,
                    type: 1,
                    title: params.href,
                    href: params.href
                }
            } else if (index === 0) {
                shareData = {
                    provider: provider,
                    scene: 'WXSceneSession',
                    ...params,
                }
            } else {
                shareData = {
                    provider: provider,
                    scene: 'WXSenceTimeline',
                    ...params,
                }
            }
            uni.share({
                type: 0,
                ...shareData,
                imageUrl: this.STATICURL+`images/logo.png`,
                success: () => {
                    // showToast('分享成功')
                    uni.showToast({
                        title: '分享成功',
                        duration: 2000,
                        icon: 'none'
                    })
                    params.callBack()
                },
                fail: (err) => {
                    console.log(JSON.stringify(err))
                        // showToast('分享失败')
                    uni.showToast({
                        title: '分享失败',
                        duration: 2000,
                        icon: 'none'
                    })
                }
            })
        },
        fail: () => {}
    })
}

const showModal = ( data={content:''}) => {
    return new Promise(function(resolve, reject) {
        uni.showModal({
            title:  '提示',
            content: data.content,
            cancelText:'取消',
            confirmText:'确定',
            success: function(res) {
                resolve(res)
            }
        });
    })
}
const Dialog = {
    alert: params => {
        return new Promise(resolve => {
            uni.showModal({
                title: '提示',
                content: params.message,
                showCancel: false,
                confirmText: params.confirmButtonText ? params.confirmButtonText : '确定',
                success: res => {
                    if (res.confirm) {
                        resolve()
                    }
                }
            })
        })
    },

    confirm: params => {
        return new Promise((resolve, reject) => {
            uni.showModal({
                title: '提示',
                content: params.message,
                showCancel: true,
                cancelText: params.cancelButtonText ? params.cancelButtonText :'取消',
                confirmText: params.confirmButtonText ? params.confirmButtonText : '确认',
                success: res => {
                    if (res.confirm) {
                        resolve()
                    } else if (res.cancel) {
                        reject()
                    }
                }
            })
        })
    }
}

const formartDate = () => {
    const date = new Date()
    const year = date.getFullYear()
    const month = parseInt(date.getMonth()) + 1 > 9 ? parseInt(date.getMonth()) + 1 : '0' + (parseInt(date.getMonth()) + 1)
    const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()

    return year + '-' + month + '-' + day
}


/**
 * 格式化时间戳 秒
 *
 * @param {Number} timestamp
 */
function fomartTime(timestamp) {
    if (!timestamp) return ''

    const date = new Date(timestamp * 1000)
    const nowDate = new Date()
    const year = date.getFullYear()
    const month = parseInt(date.getMonth()) + 1
    const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
    const hours = date.getHours() > 9 ? date.getHours() : '0' + date.getHours()
    const min = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes()

    const nowYear = nowDate.getFullYear()

    if (nowYear !== year) {
        return `${year}年${month}月${day}`
    }

    if (nowYear === year && date.getMonth() === nowDate.getMonth() && date.getDate() === nowDate.getDate()) {
        return `${hours}:${min}`
    }

    return `${month}月${day}日`
}


/**
 * 生成富文本节点
 */
function generateRichTextNode(text, type) {
    if (!text) return false
        // 如果是数组，则直接返回
    if (type === 2 || type === 'img') {
        return text
    }

    let tempStr = text
    let list = []
    let leftBracketIndex = tempStr.indexOf('face[')
    let rightBracketIndex = tempStr.indexOf(']')
    let countOfWord = 0
    Array.from(tempStr).map(item => {
        if (item != '[' && item != ']') {
            countOfWord++
        }
    })
    if (leftBracketIndex == -1 || rightBracketIndex == -1 || countOfWord == 0) { //没有emoji
        return [{
            type: 'text',
            content: tempStr
        }]
    }
    while (tempStr.length != 0) {
        leftBracketIndex = tempStr.indexOf('face[')
        rightBracketIndex = tempStr.indexOf(']')
        if (leftBracketIndex == 0) { // 开头是[
            rightBracketIndex = tempStr.indexOf(']')
            if (rightBracketIndex == -1) {
                list.push({
                    type: 'text',
                    content: tempStr
                })
            } else {
                let emojiName = tempStr.slice(0, rightBracketIndex + 1)
                if (emoji[emojiName]) { // 有效emoji
                    list.push({
                        type: 'emoji',
                        content: emoji[emojiName].img
                    })
                } else {
                    tempStr += emojiName
                    list.push({
                        type: 'text',
                        content: emojiName
                    })
                }
                tempStr = tempStr.substring(rightBracketIndex + 1, tempStr.length)
            }
        } else { // 开头不是[
            if (leftBracketIndex == -1) { // 最后全是文字
                list.push({
                    type: 'text',
                    content: tempStr.slice(0, tempStr.length)
                })
                tempStr = ''
            } else {
                list.push({
                    type: 'text',
                    content: tempStr.slice(0, leftBracketIndex)
                })
                tempStr = tempStr.substring(leftBracketIndex, tempStr.length + 1)
            }
        }
    }

    return list
}

let imgList = []
const chooseImg = (params) => {
        imgList = []
        return new Promise((resolve) => {
            uni.chooseImage({
                count: 1,
                sourceType: ['album', 'camera'],
                sizeType: ['compressed', 'original'],
                ...params,
                success: res => {
                    /* #ifdef APP-PLUS */
                    res.tempFilePaths.forEach((item) => {
                        compress(item, res.tempFilePaths.length, resolve)
                    })

                    resolve(res)
                        /* #endif */
                        /* #ifdef MP-WEIXIN */
                    resolve(res)
                        /* #endif */
                },
                fail: err => {
                    console.log(err)
                }
            })
        })
    }
    // 页面跳转
const navigateTo = (url, params = {}) => {
        let paramsStr = '';
        Object.keys(params).forEach(key => {
            paramsStr += key + '=' + params[key] + '&'
        })
        if (paramsStr != '') {
            paramsStr = paramsStr.substring(0, paramsStr.lastIndexOf('&'))
        }
        url += '?' + paramsStr
        uni.navigateTo({
            url: url
        })
    }
    //  复制
const copy = (val) => {
    uni.setClipboardData({
        data: val,
        success() {
            uni.showToast({
                title: '复制成功',
                duration: 2000,
                icon: "none",
                position: "center"
            })
        },
        fail: () => {
            uni.showToast({
                title: '复制失败',
                duration: 2000,
                icon: "none",
                position: "bottom"
            })
        }

    })
}
const compress = (img, len, resolve) => {
    let image = img

    // 压缩size
    plus.io.resolveLocalFileSystemURL(image, (entry) => {
        entry.file((file) => { // 可通过entry对象操作图片
            // console.log("getFile:" + JSON.stringify(file));
            plus.zip.compressImage({
                src: image,
                dst: image,
                width: '100%',
                height: '100%',
                quality: 60,
                overwrite: true
            }, (event) => {
                let newImg = image;
                imgList.push(newImg)
                if (len === imgList.length) {
                    resolve({
                        tempFilePaths: imgList
                    })
                }
            }, function(error) {
                // console.log('success zip****' + JSON.stringify(error));
                uni.showModal({
                    content: "分享图片太大,需要请重新选择图片!",
                    showCancel: false
                })
            });
        });
    }, (e) => {
        // console.log("Resolve file URL failed: " + e.message);
        uni.showModal({
            content: "分享图片太大,需要请重新选择图片!",
            showCancel: false
        })
    });
}

//时间戳 转 YY-mm-dd HH:ii:ss
const formatDateTime = (inputTime) => {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};

const previewImgs = (list=[],index=0)=>{
    uni.previewImage({
        urls: list,
        current:index,
        indicator:"number"
    });
};

const formatSeconds = function(value) {
    var secondTime = parseInt(value); // 秒
    var minuteTime = 0; // 分
    var hourTime = 0; // 小时
    if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
            //获取小时，获取分钟除以60，得到整数小时
            hourTime = parseInt(minuteTime / 60);
            //获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result = hourTime + '-' + minuteTime + '-' + secondTime
    return result;
}
const throttle = (func, delay) =>{
    var timer = null;
    return function() {
        var context = this;
        var args = arguments;
        if (!timer) {
            timer = setTimeout(function() {
                func.apply(context, args);
                timer = null;
            }, delay);
        }
    }
}

const uniRequest =(path = "", params = {}, method = "POST")=>{

    return new Promise((resolve, reject) => {

        uni.request({
            url: `${baseUrl}/${path}?t=${new Date().getTime()}`,
            method: method,
            data: {
                api_token: md5(
                    `${path}${formartDate()}yiling6670238160ravntyoneapp7926`
                ),
                client_id: 1,
                token,
                ...params,
            },
            success: (res) => {
                resolve(res);

            },
            fail: (err) => {
                reject(err);
            },
        });
    });
}
const authorizedPositioning = ( callBack = ()=>{}) =>{
	uni.getSystemInfo({ // 获取系统信息
			success(res) {
				let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
				let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
				if (locationEnabled == false || locationAuthorized == false) {
					// GPS 未授权
					callBack("GPSunauthorized");
				} else {
					// GPS 已授权 判断微信定位是否授权
					uni.authorize({
						scope: 'scope.userLocation',
						success() {
							// GPS已授权 微信定位已授权
							uni.getLocation({
								type: 'gcj02',
								success({
									latitude,
									longitude
								}) {
									// latitude; 纬度
									// longitude; 经度
									callBack("Authorized", {
										latitude,
										longitude
									});
								}
							});
						},
						fail() {
							// GPS已授权 微信定位未授权
							callBack("WENXINunauthorized");
							uni.showModal({
								title: '未打开小程序定位',
								content: '找不到您的位置，请开启定位',
								confirmText: '开启定位',
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										uni.openSetting(); // 打开地图权限设置
									}
								}
							});
						}
					});
				}
			}
	})
}

export  {
    showToast,
    hideLoading,
    showLoading,
    formartDate,
    previewImgs,
    chooseImg,
    generateRichTextNode,
    fomartTime,
    formatDateTime,
    showModal,
    formatSeconds,
    Dialog,
    share,
    navigateTo,
    copy,
    throttle,
    uniRequest,
	authorizedPositioning
}

